<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class IndexController extends BaseController {
    public function index(){

        $this->assign('title','后台管理中心');
        $this->display();
    }
    public function repassword(){
        $u_data = session('u_data');
        if(IS_POST){
            $data = I('post.');
            if(empty($data['oldpassword']) || empty($data['password']) || empty($data['repassword'])){
                $this->error('原密码/新密码/确认密码不能为空');
            }
            if($data['password'] != $data['repassword']){
                $this->error('新密码与确认密码不一致');
            }
            $rows = M('Admin')->where(array('a_id'=>$u_data['a_id']))->find();
            if(md5($data['oldpassword'].$rows['key']) != $rows['password']){
                $this->error('原密码不正确');
            }
            $res = M('Admin')->where(array('a_id'=>$u_data['a_id']))->setField('password',md5($data['password'].$rows['key']));
            $res!== false ?$this->success('操作成功'):$this->error('操作失败');
        }else{
            $this->assign('name',$u_data['username']);
            $this->display();
        }
    }
    public function message(){
        $data = I('get.');
        $this->assign('status',1);
        if($data['status'] != ''){
            $where['u.status'] =  $data['status'];
            $this->assign('status',$data['status']);
        }
        $jubao  = I('get.jubao');
        if(!empty($jubao)){
            $join = 'join '.C('DB_PREFIX'). 'report  as r on u.message_id = r.message_id';
            $group = 'u.message_id';
            $this->assign('jubao',$jubao);
            $this->assign('status','');
        }
       $keywords =  $data['keywords'];
        if(!empty($keywords)){
            $user_id  = M('Users')->where(array('nickname'=>$data['keywords']))->getField('user_id');
           if($user_id){
               $where['u.user_id'] = $user_id;
               $this->assign('keywords',$data['keywords']);
           }
        }
        if($data['b_time'] != '' && $data['e_time'] != ''){
            $where['u.add_time'] = array(array('gt',strtotime($data['b_time'])),array('lt',strtotime($data['e_time'])), 'and') ;
        }elseif($data['b_time'] != '' && $data['e_time'] == ''){
            $where['u.add_time'] = array('gt',strtotime($data['b_time']));
        }
        elseif($data['b_time'] == '' && $data['e_time'] != ''){
            $where['u.add_time'] = array('lt',strtotime($data['e_time']));
        }
        $this->assign('b_time',$data['b_time']);
        $this->assign('e_time',$data['e_time']);
        $count = M('Message')->alias('u')->join($join)->where($where)->count();
        $page = new Page($count,10);
        $show = $page->show();
        $list = M('Message')->field('u.*')->alias('u')->join($join)->where($where)->limit($page->firstRow,$page->listRows)->order('message_id desc')->group($group)->select();
        foreach ($list as &$l){
            $l['username'] = M('Users')->where(array('user_id'=>$l['user_id']))->getField('nickname');
            $l['con'] = msubstr($l['content'],0,15);
            $l['add_time'] = date('Y-m-d H:i:s',$l['add_time']);
            $l['is_show_name'] == 1?$l['type']='否':$l['type']='是';
            $l['status'] == 1?$l['s_type']='正常':$l['s_type']='已删除';
            $l['like_num'] = M('Like')->where(array('message_id'=>$l['message_id']))->count();
            $l['commet_num'] = M('Comment')->where(array('message_id'=>$l['message_id']))->count();
            $l['report_num'] = M('Report')->where(array('message_id'=>$l['message_id']))->count();
        }
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
    public function del_message(){
        if(IS_POST){
            $message_id = I('post.message_id');
            if(empty($message_id)){
                $this->error('数据有误');
            }
            $res = M('Message')->where(array('message_id'=>$message_id))->setField('status',0);
            $res !== false?$this->success('操作成功'):$this->error('操作失败');
        }
    }
    public function pl_del(){
        if(IS_POST){
            $message_id=trim(I('post.ids'),',');
            if(empty($message_id)){
                $this->error('请选择记录');
            }
            $res = M('Message')->where(array('message_id'=>array('in',$message_id)))->setField('status',0);
            $res !== false?$this->success('操作成功'):$this->error('操作失败');
        }
    }
    public function user(){
        $keywords = I('get.keywords');
        if(trim($keywords) != ''){
            $where['nickname'] = $keywords;
            $this->assign('keywords',$keywords);
        }
        $count = M('Users')->where($where)->count();
        $page = new Page($count,10);
        $show = $page->show();
        $list = M('Users')->where($where)->limit($page->firstRow,$page->listRows)->order('user_id desc')->select();
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
    public function message_detail(){
        $message_id = I('get.message_id');
        if(empty($message_id)){
            $this->error('数据有误');
        }
        $message = M('Message')->where(array('message_id'=>$message_id))->getField('content');
        $likes = M('like')->alias('l')->field('u.headimgurl,u.nickname')->join('left join lyb_users as u on l.user_id=u.user_id')->where(array('l.message_id'=>$message_id))->select();
        $this->assign('likes',$likes);
        $this->assign('message',$message);
        $this->display();
    }
    public function message_report(){
        $message_id = I('get.message_id');
        if($message_id == ''){
            $this->error('数据有误');
        }
        $where['message_id'] = $message_id;
        $count = M('Report')->where($where)->count();
        $page = new Page($count,10);
        $show = $page->show();
        $list = M('Report')->where($where)->limit($page->firstRow,$page->listRows)->order('report_id desc')->select();
        foreach ($list as &$l){
            $user = M('Users')->field('nickname,headimgurl')->where(array('user_id'=>$l['user_id']))->find();
            $l['nickname'] = $user['nickname'];
            $l['headimgurl'] = $user['headimgurl'];
            $l['time'] = date('Y-m-d H:i:s');
        }
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->assign('title','举报详情');
        $this->display();
    }
    public function message_comment(){
        $message_id = I('get.message_id');
        if($message_id == ''){
            $this->error('数据有误');
        }
        $where['message_id'] = $message_id;
        $count = M('Comment')->where($where)->count();
        $page = new Page($count,10);
        $show = $page->show();
        $list = M('Comment')->where($where)->limit($page->firstRow,$page->listRows)->order('comment_id desc')->select();
        foreach ($list as &$l){
            $user = M('Users')->field('nickname,headimgurl')->where(array('user_id'=>$l['user_id']))->find();
            $l['nickname'] = $user['nickname'];
            $l['headimgurl'] = $user['headimgurl'];
            $l['time'] = date('Y-m-d H:i:s');
        }
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->assign('title','评论详情');
        $this->display();
    }
    public function tickling(){
        $data = I('get.');
        $keywords =  $data['keywords'];
        if(!empty($keywords)){
            $user_id  = M('Users')->where(array('nickname'=>$data['keywords']))->getField('user_id');
            if($user_id){
                $where['user_id'] = $user_id;
                $this->assign('keywords',$data['keywords']);
            }
        }
        if($data['b_time'] != '' && $data['e_time'] != ''){
            $where['add_time'] = array(array('gt',strtotime($data['b_time'])),array('lt',strtotime($data['e_time'])), 'and') ;
        }elseif($data['b_time'] != '' && $data['e_time'] == ''){
            $where['add_time'] = array('gt',strtotime($data['b_time']));
        }
        elseif($data['b_time'] == '' && $data['e_time'] != ''){
            $where['add_time'] = array('lt',strtotime($data['e_time']));
        }
        $this->assign('b_time',$data['b_time']);
        $this->assign('e_time',$data['e_time']);
        $count = M('Tickling')->where($where)->count();
        $page = new Page($count,10);
        $show = $page->show();
        $list = M('Tickling')->where($where)->limit($page->firstRow,$page->listRows)->order('t_id desc')->select();
        $this->assign('list',$list);
        $this->assign('show',$show);
        $this->display();
    }
    public function set(){
        if(IS_POST){
           $data = I('post.');
           if($data['like_num'] <= 0 || $data['page_num'] <= 0){
               $this->error('数量必须为大于0的整数');
           }
           $res = M('Set')->save($data);
            $res !== false?$this->success('操作成功'):$this->error('操作失败');
        }else{
            $row = M('Set')->find();
            $this->assign('row',$row);
            $this->display();
        }
    }
}